package com.villaday.idf.activity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import androidx.appcompat.widget.Toolbar;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;
import com.villaday.idf.R;
import com.villaday.idf.base.BaseActivity;
import com.villaday.idf.util.ToolBarUtil;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/*
* 我的积分
* */
public class MyIntegralActivity extends BaseActivity {

    @BindView(R.id.toolbar_text)
    Toolbar toolbarText;
    @BindView(R.id.tv)
    TextView tv;



    @Override
    protected int getViewResId() {
        return R.layout.activity_my_integral;
    }

    @Override
    protected void init() {
        ToolBarUtil.initToolBar(toolbarText, "我的积分", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });

        //这里面的resource就是fromhtml函数的第一个参数里面的含有的url
        Html.ImageGetter imgGetter = new Html.ImageGetter() {
            public Drawable getDrawable(String source) {
                Log.i("RG", "source---?>>>" + source);
                Drawable drawable = null;
                URL url;
                try {
                    url = new URL(source);
                    Log.i("RG", "url---?>>>" + url);
                    drawable = Drawable.createFromStream(url.openStream(), ""); // 获取网路图片
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
                        drawable.getIntrinsicHeight());
                Log.i("RG", "url---?>>>" + url);
                return drawable;
            }
        };

        tv.setText(Html.fromHtml("<span style=\\\"color:#333;font-size:15px;\\\"><strong>砍价师服务介绍</strong></span><br/><span align=\\\"right\\\" style=\\\"color:#333;font-size:15px;\\\">我们不是中介。</span><br/> <span style=\\\"color:#333;font-size:15px;\\\">我们是一群愿意站在买房人一边的，地产业内人士。</span><br/><br/><span style=\\\"color:#333;font-size:15px;\\\"><strong>砍不下来，不要钱！</strong></span><br/><span style=\\\"color:#333;font-size:15px;\\\">类似你请个律师，完全站在你的立场，帮你谈判。我们发心，用立场和专业，改变中国买房人的被动、挨宰局面！</span><br/><br/><span style=\\\"color:#333;font-size:15px;\\\"><strong>服务流程：</strong></span><br/><span style=\\\"color:#333;font-size:15px;\\\">1.砍前培训。砍价师教你和中介、业主交流，哪些话能说，哪些话不能说；</span><br/><span style=\\\"color:#333;font-size:15px;\\\">2.选砍价师。和砍价师约见，确认服务，并做各方信息梳理，确定谈判策略。</span><br/><span style=\\\"color:#333;font-size:15px;\\\">3.现场谈判。砍价师陪你去现场，协助把控谈判进程；在你砍不动时，再继续全力争取最好价格。</span><br/><br/><span style=\\\"color:#333;font-size:15px;\\\"><strong>收费标准：</strong></span><br/><span style=\\\"color:#333;font-size:15px;\\\">记住！砍价是由你自己先砍，砍不动时再由砍价师继续砍；由砍价师多砍下的部分，才按照下列标准收费：</span><br/><span style=\\\"color:#333;font-size:15px;\\\"><img src=\"http://wx4.sinaimg.cn/mw600/0076BSS5ly1ftt1olthz8j30m90xcq9y.jpg\"></span><span style=\\\"color:#333;font-size:15px;\\\"><strong>砍不下来，不收费！</strong></span><br/><br/><span style=\\\"color:#333;font-size:15px;\\\"><strong>举个例子：</strong></span><br/><span style=\\\"color:#333;font-size:15px;\\\">李先生看好一套房子，经过自己努力将价格砍到300万，砍价师在李先生的基础上将价格谈到270万，成功砍下30万，其中0~5万元阶梯价格部分为5万元，5~10万元阶梯价格部分为5万元，10万元以上阶梯价格部分为20万元，则</span><br/><span style=\\\"color:#333;font-size:15px;\\\">",new URLImageGetter(tv,this),null));
    }

    public class URLImageGetter implements Html.ImageGetter {
        Context c;
        TextView tv_image;
        private List<Target> targets = new ArrayList<>();

        public URLImageGetter(TextView t, Context c) {
            this.tv_image = t;
            this.c = c;
            tv_image.setTag(targets);
        }

        @Override
        public Drawable getDrawable(final String source) {
            final URLDrawable urlDrawable = new URLDrawable();
            final Target target = new Target() {
                @Override
                public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                    Drawable drawable = new BitmapDrawable(bitmap);
                    drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
                    urlDrawable.setDrawable(drawable);
                    urlDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
                    tv_image.invalidate();
                    tv_image.setText(tv_image.getText());
                }

                @Override
                public void onBitmapFailed(Exception e, Drawable errorDrawable) {
                    errorDrawable.setBounds(0, 0, errorDrawable.getIntrinsicWidth(), errorDrawable.getIntrinsicHeight());
                    urlDrawable.setBounds(0, 0, errorDrawable.getIntrinsicWidth(), errorDrawable.getIntrinsicHeight());
                    urlDrawable.setDrawable(errorDrawable);
                    tv_image.invalidate();
                }

                @Override
                public void onPrepareLoad(Drawable placeHolderDrawable) {
                    placeHolderDrawable.setBounds(0, 0, placeHolderDrawable.getIntrinsicWidth(), placeHolderDrawable.getIntrinsicHeight());
                    urlDrawable.setBounds(0, 0, placeHolderDrawable.getIntrinsicWidth(), placeHolderDrawable.getIntrinsicHeight());
                    urlDrawable.setDrawable(placeHolderDrawable);
                    tv_image.invalidate();
                }
            };

            targets.add(target);
           loadPlaceholder(c, source, target);
            return urlDrawable;
        }

        public  void loadPlaceholder(Context context, String url, Target target) {

            Picasso picasso = new Picasso.Builder(context).loggingEnabled(true).build();
            picasso.load(url)
                    .placeholder(R.drawable.img_order)
                    .error(R.drawable.img_order)

                    .into(target);
        }


    }


    public class URLDrawable extends BitmapDrawable {
        private Drawable drawable;

        @Override
        public void draw(Canvas canvas) {
            if (drawable != null) {
                drawable.draw(canvas);
            }
        }

        public void setDrawable(Drawable drawable) {
            this.drawable = drawable;
        }

    }






}


